var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusArr: ['待开工', '施工中', '政策处理', '竣工']
  },
  // 获取地址前缀
  getAddress() {
    var IAP = getApp().globalData.host;
    this.setData({
      IAP: IAP
    })
  },
  // 开始时间
  startDateChange: function(e) {
    this.setData({
      startdate: e.detail.value,
    })
    var startTime = new Date(this.data.startdate.replace(/-/g, "/")).getTime();
    var consPeriod = (this.data.consPeriod ? this.data.consPeriod : 0) * 24 * 3600 * 1000;
    var endTime = (startTime + consPeriod) / 1000
    console.log()
    this.setData({
      enddate: util.formatTimeTwo(endTime, 'Y-M-D')
    })
  },
  // 结束时间
  endDateChange: function(e) {
    this.setData({
      enddate: e.detail.value
    })
  },
  // 施工周期发生变化函数
  consPeriodChange(e) {
    var startTime = new Date(this.data.startdate.replace(/-/g, "/")).getTime();
    var consPeriod = e.detail.value * 24 * 3600 * 1000;
    var endTime = (startTime + consPeriod) / 1000
    this.setData({
      enddate: util.formatTimeTwo(endTime, 'Y-M-D'),
      consPeriod: e.detail.value
    })
  },
  // 下一步
  nextSubmit(e) {
    var startTime = util.formatTimeTwo((new Date(this.data.startdate.replace(/-/g, "/")).getTime()) / 1000, 'Y-M-D h:m:s');
    var endTime = util.formatTimeTwo((new Date(this.data.enddate.replace(/-/g, "/")).getTime()) / 1000, 'Y-M-D h:m:s');
    if (this.data.projectId) {
      wx.request({
        url: this.data.IAP + 'project/update',
        method: "POST",
        data: {
          projectId: this.data.projectId,
          projectName: e.detail.value.projectName,
          deptid: this.data.deptid,
          consPeriod: e.detail.value.consPeriod,
          consManager: wx.getStorageSync('userid'),
          startTime: startTime,
          endTime: endTime,
          status: this.data.index1 ? this.data.index1 : this.data.engProject.status
        },
        header: {
          "token": wx.getStorageSync('token')
        },
        success: res => {
          if (res.data.code == 0) {
            wx.reLaunch({
              url: '../index/index',
              success: res => {
                wx.showToast({
                  title: '工程修改成功',
                })
              }
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    } else {
      wx.request({
        url: this.data.IAP + 'project/save',
        header: {
          "token": wx.getStorageSync('token')
        },
        method: "POST",
        data: {
          projectName: e.detail.value.projectName,
          deptid: this.data.departArr[this.data.index].id,
          consPeriod: e.detail.value.consPeriod,
          consManager: wx.getStorageSync('userid'),
          startTime: startTime,
          endTime: endTime,
          status: 0
        },
        success: res => {
          if (res.data.code == 0) {
            wx.reLaunch({
              url: '../index/index',
              success: res => {
                wx.showToast({
                  title: '工程添加成功',
                })
              }
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }

  },
  // 获取个人信息
  getPersonalInfo() {
    wx.request({
      url: this.data.IAP + 'user/info/',
      header: {
        token: wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.code == 0) {
          this.setData({
            userInfo: res.data.user
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  // 获取该公司所属的全部部门
  getDepartment() {
    wx.request({
      url: this.data.IAP + 'project/deptlist',
      header: {
        "token": wx.getStorageSync('token')
      },
      success: res => {
        this.setData({
          departArr: res.data
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求发送失败',
          icon: 'none'
        })
      }
    })
  },
  // 切换部门
  departmentChange(e) {
    this.setData({
      index: e.detail.value,
      deptid: this.data.departArr[e.detail.value].id
    })
  },
  // 切换施工状态
  departmentChange1(e) {
    this.setData({
      index1: e.detail.value
    })
  },
  // 编辑时根据工程ID获取工程信息
  getInfoById() {
    wx.request({
      url: this.data.IAP + 'project/info/' + this.data.projectId,
      header: {
        "token": wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.code == 0) {
          this.setData({
            engProject: res.data.engProject
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getAddress();
    this.getDepartment();
    this.getPersonalInfo();
    var TIME = util.formatTime(new Date()).substring(0, 10);

    if (options.eid) {
      wx.setNavigationBarTitle({
        title: '编辑',
      })
      this.setData({
        projectId: options.eid,
      })
    } else {
      wx.setNavigationBarTitle({
        title: '新建',
      })
      this.setData({
        startdate: TIME,
        enddate: TIME,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.projectId) {
      this.getInfoById();
      setTimeout(() => {
        this.setData({
          startdate: this.data.engProject.startTime,
          enddate: this.data.engProject.endTime,
          consPeriod: this.data.engProject.consPeriod,
          deptid: this.data.engProject.deptid
        });
      }, 300)
    } else {
      return;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})